var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// const { jwtAuth } = require('tools-factory-tang/jwt');

require("./db/index");//执行db的index.js文件

// 引入路由

// 关于用户的路由
var usersRouter = require(`./routes/users`);
// 管理员的路由
var adminRouter = require(`./routes/admin`);
// 关于用户的路由
var seckillRouter = require(`./routes/seckill`);
// 管理员的路由
// 购物车路由
var shoppingCartRouter = require(`./routes/shoppingCart`);
// 购物车路由
var orderRouter = require(`./routes/order`);
//关于类型的路由
var tatolTypeRouter = require(`./routes/totalType`)
//关于类型的路由
var tatolTypeRouter = require(`./routes/totalType`);
// 关于商品的路由
var commodityRouter = require("./routes/commodity")
//关于自提点的路由
var addressRouter = require("./routes/address")
// 关于订单的路由
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
require("./swagger")(app);


// 添加api
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 引入token验证，平常是false关闭不验证状态
// app.use(jwtAuth);


//用户
app.use("/users", usersRouter);
// admin管理员
app.use("/admin", adminRouter);
app.use("/seckill", seckillRouter)
// 购物车路由
app.use("/shoppingCart", shoppingCartRouter);
// 挂载订单
app.use("/order", orderRouter);
app.use("/totalType", tatolTypeRouter);

//商品路由
app.use("/commodity", commodityRouter);

//自提点
app.use("/address", addressRouter)



app.use(function (req, res, next) {
  next(createError(404));
});

app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
module.exports = app;
